package com.NeuglsWorkStudio.UI.FormElement.Component
{
	import com.NeuglsWorkStudio.Colors.ColorUtility;
	import com.NeuglsWorkStudio.Colors.HSL;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TLabelConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TRadioBoxConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TRadioGroupConfig;
	
	import flash.display.Sprite;

	public class TRadioGroup extends TComponent
	{
		private var _FContainer:Sprite;
		private var _FButtons:Vector.<TRadioBox>;
		private var _FCaption:TLabel;
		private var _FIndex:int=0;
		public static var RadioBoxMarginX:Number=5;
		public static var RadioBoxMarginY:Number=5;

		public function get OnIndexChanged():Function
		{
			return _FOnIndexChanged;
		}

		public function set OnIndexChanged(value:Function):void
		{
			_FOnIndexChanged = value;
		}

		private var _FOnIndexChanged:Function;
		
		public function TRadioGroup(AConfig:TConfig)
		{
			super(AConfig);
		}
		
		public function get Index():int
		{
			return _FIndex;
		}

		public function set Index(value:int):void
		{
			_FIndex = value;
			MakeIndex(value);
		}

		protected override function Init():void{
			super.Init();
			
			_FContainer=new Sprite();
			_FButtons=new Vector.<TRadioBox>();
			_FOnIndexChanged=RadioGroupConfig().OnIndexChanged;
			
			var labelConfig:TLabelConfig=new TLabelConfig();
			labelConfig.Caption=RadioGroupConfig().Caption;
			labelConfig.Color=ColorUtility.GetColorFromBaseColor(RadioGroupConfig().BaseColor);
			labelConfig.Font="Tahoma";
			labelConfig.Size=12;
			labelConfig.Name="RadioGroupCaption";
			_FCaption=new TLabel(labelConfig);
			this.addChild(_FContainer);
			Paint();
		}
		
		private function RadioGroupConfig():TRadioGroupConfig{
			return _FConfig as TRadioGroupConfig;
		}
		
		protected function Paint():void{
			var RoundRadius:Number=5;
			var lineSize:Number=1;
			var lastY:Number=10;
			//draw roundrect
			with(_FContainer.graphics){
				clear();
				lineStyle(lineSize,0x010101);
				beginFill(0,0);
				drawRoundRect(0,lastY,
					RadioGroupConfig().Width-2*lineSize,
					RadioGroupConfig().Height-2*lineSize,
					2*RoundRadius,2*RoundRadius
				);
				lastY+=lineSize;
				
				lineStyle(1,ColorUtility.GetBorderColorFromBaseColor(RadioGroupConfig().BaseColor));
				drawRoundRect(lineSize,lastY,
					RadioGroupConfig().Width-4*lineSize,
					RadioGroupConfig().Height-4*lineSize,
					2*RoundRadius,2*RoundRadius
				);
				lastY+=lineSize;
			}
			
			//_FContainer.addChild(_FCaption);
			_FCaption.x=_FConfig.Width>10?10:0;
			_FCaption.y=lastY-(_FCaption.height)/2;
			
			lastY+=RadioBoxMarginY;
			for(var i:int=0;i<_FButtons.length;i++){
				with(_FContainer.addChild(_FButtons[i])){
					x=RadioBoxMarginX;
					y=lastY;
				}
				
				lastY+=_FButtons[i].height+RadioBoxMarginY;
			}
		}
		
		/**After you add all the radiobox, please call the Update method**/
		public function AddRadioBox(RadioBoxConfig:TRadioBoxConfig):int{
			var result:int= _FButtons.push(new TRadioBox(RadioBoxConfig));
			_FButtons[result-1].OnClick=function():void{
				OnRadioBoxClick(result-1);
			}
			return result;
		}
		
		/***Update the interface**/
		public function Update():void{
			Paint();
		}
		
		public function AddRadioBoxs(RadioBoxs:Array):void{
			for(var i:int=0;i<RadioBoxs.length;i++){
				if (!RadioBoxs[i] is TRadioBoxConfig){
					throw new Error('The member of the array paraments should be instance of TRadioBoxConfig');
				} 
					
				AddRadioBox(RadioBoxs[i]);
			}
			
			Paint();
		}
		
		private function OnRadioBoxClick(ID:int):void{
			_FIndex=ID;
			MakeIndex(ID);
			if(_FOnIndexChanged!=null){
				_FOnIndexChanged(ID);
			}
		}
		
		protected function MakeIndex(ID:int):void{
			for(var i:int=0;i<_FButtons.length;i++){
				_FButtons[i].Checked=i==ID;
			}
		}
		
		public function FindRadioBoxByID(ID:Number):TRadioBox{
			return _FButtons[ID];
		}
		
		public function FindRadioBoxByName(AName:String):TRadioBox{
			return _FContainer.getChildByName(AName) as TRadioBox;
		}
	}
}